package cn.javadog.smartbreeding.record.service.impl;

import cn.javadog.smartbreeding.core.dto.PageDTO;
import cn.javadog.smartbreeding.core.service.TemperatureRecordService;
import cn.javadog.smartbreeding.core.vo.PageVo;
import cn.javadog.smartbreeding.core.vo.PageVo.Pagination;
import cn.javadog.smartbreeding.record.converter.TemperatureRecordConverter;
import cn.javadog.smartbreeding.core.dto.TemperatureRecordDTO;
import cn.javadog.smartbreeding.record.entity.TemperatureRecord;
import cn.javadog.smartbreeding.record.mapper.TemperatureRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * @author 余勇
 */
@Service
@com.alibaba.dubbo.config.annotation.Service
public class TemperatureRecordServiceImpl implements TemperatureRecordService {

	@Autowired
	private TemperatureRecordMapper mapper;
	@Autowired
	private TemperatureRecordConverter converter;

	@Override
	public String save(TemperatureRecordDTO dto) {
		TemperatureRecord temperatureRecord = converter.dto2entity(dto);
		mapper.insert(temperatureRecord);
		return temperatureRecord.getId();
	}

	@Override
	public PageVo list(PageDTO dto) {
		IPage<TemperatureRecord> page = mapper.selectPage(new Page<>(dto.getCurrentPage(), dto.getPageSize()),
			Wrappers.emptyWrapper());
		return PageVo.builder()
			.pagination(Pagination.builder()
					.current(page.getCurrent())
					.size(page.getSize())
					.total(page.getTotal())
					.build())
			.list(converter.entity2dto(page.getRecords()))
			.build();
	}
}
